import 'package:flutter/material.dart';
import '../../utils/regex_util.dart';

// 表单验证相关常量
const int minPhoneLength = 7;
const int maxPhoneLength = 15;
const int minNameLength = 2;
const int maxNameLength = 50;
final RegExp passwordRegex = RegExp(
  r'^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};:",.<>/?\\|]{8,16}$',
);

/// 获取通用的输入框装饰
InputDecoration getCommonInputDecoration({
  required String hintText,
  required String labelText,
  Widget? suffixIcon,
}) {
  return InputDecoration(
    contentPadding: const EdgeInsets.symmetric(vertical: 15, horizontal: 15),
    hintText: hintText,
    labelText: labelText,
    hintStyle: TextStyle(fontSize: 14, color: Colors.grey[400]),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.grey.shade300),
      borderRadius: BorderRadius.circular(8),
    ),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.black),
      borderRadius: BorderRadius.circular(8),
    ),
    filled: true,
    fillColor: Colors.grey.shade50,
    labelStyle: TextStyle(color: Colors.grey),
    suffixIcon: suffixIcon,
  );
}

/// 通用输入框包装组件
Widget buildFormItem(Widget child) {
  return Padding(
    padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 12),
    child: Row(children: [Expanded(child: child)]),
  );
}

/// 邮箱验证器
String? validateEmail(String? value) {
  if (value == "") {
    return "Please enter your email address";
  }
  if (!RegexUtil.isEmail(value!)) {
    return "Please enter a valid email address";
  }
  return null;
}

/// 密码验证器
String? validatePassword(String? value) {
  if (value == "") {
    return 'Please Enter password';
  }
  if (!passwordRegex.hasMatch(value!)) {
    return 'Password must be 8-16 characters and can include letters, numbers, and special characters';
  }
  return null;
}

/// 确认密码验证器
String? validateConfirmPassword(String? value, String password) {
  if (value == "") {
    return 'Please confirm your password';
  } else if (value != password) {
    return "Passwords do not match";
  }
  return null;
}

/// 手机号验证器
String? validatePhone(String? value) {
  if (value == "") {
    return "Please Enter mobile phone number";
  }
  if (value!.length < minPhoneLength || value.length > maxPhoneLength) {
    return "Mobile length must be between $minPhoneLength and $maxPhoneLength characters";
  }
  return null;
}

/// 验证码验证器
String? validateVerificationCode(String? value) {
  if (value == "") {
    return "Please enter verification code";
  }
  if (value!.length != 6) {
    return "Verification code must be 6 digits";
  }
  return null;
}

/// 用户名验证器
String? validateUserName(String? value) {
  if (value == null || value.isEmpty) {
    return "Please enter account name";
  }
  if (value.length < minNameLength || value.length > maxNameLength) {
    return "Account name length must be between $minNameLength and $maxNameLength characters";
  }
  return null;
}

/// 全名验证器
String? validateFullName(String? value) {
  if (value == null || value.isEmpty) {
    return "Please enter your full name";
  }
  if (value.length < minNameLength || value.length > maxNameLength) {
    return "Full name length must be between $minNameLength and $maxNameLength characters";
  }
  return null;
}
